#!/bin/bash

# Unified Performance Test Runner
#
# This script provides a centralized way to run performance tests for both
# CUDA and HIP/DCU implementations. It supports running tests for a specific
# platform or for both.

# --- Configuration ---
# Test parameters
ARRAY_SIZES=(1024 65536 1048576)
#SUB_TASKS=(8 16 32 64 128 256 512)
SUB_TASKS=(1)
THREAD_COUNTS=(64 128 256 512)
TEST_MODES=("sequential" "multistream" "overlapped" "masquerading")
RUNS=5

# --- Helper Functions ---
show_usage() {
    echo "Usage: $0 [OPTIONS]"
    echo ""
    echo "Options:"
    echo "  --platform <name>  Specify the platform to test: 'cuda', 'hip', or 'all' (default)."
    echo "  --help             Show this help message."
    echo ""
    echo "Examples:"
    echo "  $0                    # Run tests for both CUDA and HIP"
    echo "  $0 --platform cuda    # Run only CUDA tests"
    echo "  $0 --platform hip     # Run only HIP/DCU tests"
}

# Function to run a test configuration
run_test() {
    local platform=$1
    local program_name=$2
    local array_size=$3
    local sub_tasks=$4
    local threads=$5
    local mode=$6

    local program_path="./bin/${program_name}"
    if [ ! -f "${program_path}" ]; then
        echo "  ✗ Error: Program '${program_path}' not found. Build it with 'make ${platform}'."
        return 1
    fi

    local output_dir="output/${platform}"
    mkdir -p "${output_dir}"
    local output_file="${output_dir}/${platform}_${mode}_a${array_size}_t${sub_tasks}_b${threads}.log"

    echo "  - Testing: Platform=${platform}, Mode=${mode}, Array=${array_size}, Tasks=${sub_tasks}, Threads=${threads}"

    "${program_path}" \
        --array-size "${array_size}" \
        --sub-tasks "${sub_tasks}" \
        --threads "${threads}" \
        --mode "${mode}" \
        --runs "${RUNS}" \
        --verbose > "${output_file}" 2>&1

    if [ $? -eq 0 ]; then
        echo "    ✓ Test completed successfully."
    else
        echo "    ✗ Test failed. Check log: ${output_file}"
    fi
}

# --- Main Execution ---
main() {
    local platform_to_run="all"

    # Parse command-line arguments
    while [[ "$#" -gt 0 ]]; do
        case $1 in
            --platform) platform_to_run="$2"; shift ;;
            --help) show_usage; exit 0 ;;
            *) echo "Unknown parameter passed: $1"; show_usage; exit 1 ;;
        esac
        shift
    done

    echo "Starting Unified Performance Test Suite at $(date)"
    echo "================================================="

    # Run CUDA tests
    if [[ "${platform_to_run}" == "cuda" || "${platform_to_run}" == "all" ]]; then
        echo ""
        echo "Running CUDA Tests..."
        echo "---------------------"
        test_count=0
        for array_size in "${ARRAY_SIZES[@]}"; do
            for sub_tasks in "${SUB_TASKS[@]}"; do
                for threads in "${THREAD_COUNTS[@]}"; do
                    for mode in "${TEST_MODES[@]}"; do
                        ((test_count++))
                        echo "[$test_count] CUDA Test Configuration..."
                        run_test "cuda" "cuda_performance" "${array_size}" "${sub_tasks}" "${threads}" "${mode}"
                    done
                done
            done
        done
        echo "CUDA tests completed: ${test_count} configurations tested."
    fi

    # Run HIP tests
    if [[ "${platform_to_run}" == "hip" || "${platform_to_run}" == "all" ]]; then
        echo ""
        echo "Running HIP/DCU Tests..."
        echo "------------------------"
        test_count=0
        for array_size in "${ARRAY_SIZES[@]}"; do
            for sub_tasks in "${SUB_TASKS[@]}"; do
                for threads in "${THREAD_COUNTS[@]}"; do
                    for mode in "${TEST_MODES[@]}"; do
                        ((test_count++))
                        echo "[$test_count] HIP Test Configuration..."
                        run_test "hip" "hip_performance" "${array_size}" "${sub_tasks}" "${threads}" "${mode}"
                    done
                done
            done
        done
        echo "HIP/DCU tests completed: ${test_count} configurations tested."
    fi

    echo ""
    echo "================================================="
    echo "Unified Performance Test Suite finished at $(date)"
    echo "Next Step: Run 'python3 scripts/analyze.py' to process the results."
}

main "$@"
